package com.cms.web.queryAnalyse;

import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.metadata.biz.MetaDataServiceMgr;
import com.metadata.bizmodel.SYS_MODEL;
import com.mrp.persistence.queryAnalyse.pageQuery.bean.EMPPageQuery;
import com.web.BaseController;

import com.cms.biz.queryAnalyse.CMSQueryAnalyseServiceMgr;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.builder.meta.bizmodel.SYS_MDL_CTN;
import com.efounder.eai.data.JParamObject;
import com.efounder.eai.data.JResponseObject;
import com.efounder.form.EFFormDataModel;
import com.efounder.form.server.provider.FormModelDataProvider;
import com.enums.EMPQueryParamEnum;

import dwz.common.util.DateUtil;

@Controller
@RequestMapping(value="/cmsSCTZQueryAnalyse")
public class EMPCMSSCTZQueryAnalyseController extends BaseController{
	@Autowired
	private                CMSQueryAnalyseServiceMgr                       queryAnalyseServiceMgr;
	
	@Autowired
	private                       MetaDataServiceMgr                           metaDataServiceMgr;
	
	@Autowired
	private                    FormModelDataProvider                                formModelUtil;
	
	@Autowired
	public HttpSession session;

	/**
	 * 生产单查询
	 * @param F_CKBH
	 * @param model
	 * @return
	 */
	@RequestMapping("/SCTZAnalyseQuery")
	public String SCTZQueryAnalyse(@RequestParam String F_QSRQ, @RequestParam String F_JZRQ, @RequestParam String F_HTBH, 
			                       @RequestParam String F_XMBH, @RequestParam String F_TXM, EMPPageQuery page, 
			                       @RequestParam String F_HTMC, @RequestParam String F_XMMC, Model model) {
		JParamObject           po = JParamObject.Create();
		EFDataSet    queryDataSet = null;
		EFRowSet      queryRowSet = null;
		String         BILL_WHERE = " SCTZXX.F_HTBH like '" + F_HTBH + "%' and SCTZXX.F_XMBH like '" + F_XMBH + "%' and "
		                          + " SCTZXX.F_ZDRQ >= '" + F_QSRQ + "' and SCTZXX.F_ZDRQ <= '" + F_JZRQ + "' and F_DJZT = '1'";
		
		if(!F_TXM.equals("")) {
			BILL_WHERE += " and SCTZXX.F_KJQJ = '" + F_TXM.substring(0, 6) + "' and SCTZXX.F_DJBH = '" + F_TXM.substring(6) + "'";
		}	
		
		po.SetValueByParamName("CTN_FCT1", "SCTZXX");
		po.SetValueByParamName("BILL_WHERE", BILL_WHERE);
		po.SetValueByParamName("BILL_ORDER", "SCTZXX.F_KJQJ desc, SCTZXX.F_DJBH desc");
		po.setValue(EMPQueryParamEnum.PAGEQUERY, page);
		
		JResponseObject        RO = queryAnalyseServiceMgr.searchSCTZ(po);
		queryRowSet = (EFRowSet) RO.getResponseObject();
		
		queryDataSet = queryRowSet.getDataSet(EMPQueryParamEnum.QUERYRESULT);
		page = (EMPPageQuery) queryRowSet.getExtProperty(EMPQueryParamEnum.PAGEQUERY, null);
		model.addAttribute("queryDataSet", queryDataSet);
		model.addAttribute("page", page);
		model.addAttribute("F_QSRQ", F_QSRQ);
		model.addAttribute("F_JZRQ", F_JZRQ);
		model.addAttribute("F_HTBH", F_HTBH);
		model.addAttribute("F_XMBH", F_XMBH);
		model.addAttribute("F_HTMC", F_HTMC);
		model.addAttribute("F_XMMC", F_XMMC);
		model.addAttribute("F_TXM", F_TXM);
		return "/cms/queryAnalyse/SCTZ/sctzQueryList";
	}
	
	/**
	 * 材料明细查询参数选择界面
	 * @param F_CKBH
	 * @param model
	 * @return
	 */
	@RequestMapping("/preQueryForm/SCTZBMWQRAnalyseQueryPage")
	public String materialQueryPage(@RequestParam String action, @RequestParam String navTabId, @RequestParam String title, @RequestParam String returnURL, Model model) {

		model.addAttribute("F_QSRQ", DateUtil.date3String("yyyy") + "-01-01");
		model.addAttribute("F_JZRQ", DateUtil.date2String(DateUtil.getLastDayOfMonth(Calendar.getInstance()), "yyyy-MM-dd"));		
		model.addAttribute("action", action);
		model.addAttribute("navTabId", navTabId);
		model.addAttribute("title", title);
		return returnURL;
	}
	
	/**
	 * 生产单查询
	 * @param F_CKBH
	 * @param model
	 * @return
	 */
	@RequestMapping("/SCTZBMWQRAnalyseQuery")
	public String SCTZBMWQRAnalyseQuery(@RequestParam String F_QSRQ, @RequestParam String F_JZRQ, @RequestParam String F_HTBH, 
			                            @RequestParam String F_XMBH, @RequestParam String F_BMBH, @RequestParam String F_TXM, @RequestParam String F_QRZT, EMPPageQuery page, 
			                            @RequestParam String F_HTMC, @RequestParam String F_XMMC, @RequestParam String F_BMMC, Model model) {
		JParamObject           po = JParamObject.Create();
		EFDataSet    queryDataSet = null;
		EFRowSet      queryRowSet = null;

		po.SetValueByParamName("F_QSRQ", F_QSRQ);
		po.SetValueByParamName("F_JZRQ", F_JZRQ);
		po.SetValueByParamName("F_BMBH", F_BMBH);
		po.SetValueByParamName("F_HTBH", F_HTBH);
		po.SetValueByParamName("F_XMBH", F_XMBH);
		po.SetValueByParamName("F_TXM", F_TXM);
		po.SetValueByParamName("F_QRZT", F_QRZT);
		po.setValue(EMPQueryParamEnum.PAGEQUERY, page);
		
		JResponseObject        RO = queryAnalyseServiceMgr.searchSCTZBMWQR(po);
		queryRowSet = (EFRowSet) RO.getResponseObject();
		
		queryDataSet = queryRowSet.getDataSet(EMPQueryParamEnum.QUERYRESULT);
		page = (EMPPageQuery) queryRowSet.getExtProperty(EMPQueryParamEnum.PAGEQUERY, null);
		model.addAttribute("queryDataSet", queryDataSet);
		model.addAttribute("page", page);
		model.addAttribute("F_QSRQ", F_QSRQ);
		model.addAttribute("F_JZRQ", F_JZRQ);
		model.addAttribute("F_BMBH", F_BMBH);
		model.addAttribute("F_HTBH", F_HTBH);
		model.addAttribute("F_XMBH", F_XMBH);
		model.addAttribute("F_BMMC", F_BMMC);
		model.addAttribute("F_HTMC", F_HTMC);
		model.addAttribute("F_XMMC", F_XMMC);
		model.addAttribute("F_TXM", F_TXM);
		model.addAttribute("F_QRZT", F_QRZT);
		return "/cms/queryAnalyse/SCTZ/bmwqrQueryList";
	}
	
	/**
	 * 评审合同编辑
	 * @param model
	 * @return
	 */
	@RequestMapping("/productNotice/show/{F_GUID}")
	public String productNoticeEdit(@PathVariable("F_GUID") String F_GUID, HttpServletRequest request, Model model) {
		JParamObject           PO = JParamObject.Create();
		EFFormDataModel formModel;

		try {
			//加载模型数据
			PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "SCTZModel");
			PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "FormProvider");
			PO.SetValueByParamName("BILL_WHERE", " ####.F_GUID = '" + F_GUID + "'");
			PO.SetValueByParamName(SYS_MDL_CTN._BIZ_CTN_TYPE_JIDS_ + "_SCTZMX_EXT_COLUMN", " F_CPBH.F_GGXH as F_GGXH");
			formModel = formModelUtil.loadForm(PO);
			EFDataSet itemCTN = metaDataServiceMgr.getSYS_MDL_CTN("SCTZModel", SYS_MDL_CTN._BIZ_CTN_TYPE_JIDS_);
			EFRowSet rowset = formModel.getBillDataSet().getRowSet(0);
			EFDataSet dataset = rowset.getDataSet(itemCTN.getRowSet(0).getString("CTN_FCT1", ""));

			model.addAttribute("dictObject", rowset);
			model.addAttribute("dataset", dataset);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/cms/queryAnalyse/SCTZ/form/form";
	}	
}
